/** \file    io_error.h
  * \brief   Exceptions hierarchy thrown during images i/o operations.
  * \date    2003
  * \author  Fedor Chelnokov, Pavel Agapov
  * \version $Header: /home/cscom1r4/cvsroot/cs/include/imglib/io_error.h,v 1.1 2004/02/04 14:43:24 cscom1r4 Exp $
  */

#ifndef _io_error_h_
#define _io_error_h_

#include <exception>
#include <string>

namespace img {

class io_error: public std::exception {
public:
	io_error(const char * reason, const char * filename) 
		: _reason(reason), _filename(filename) {}
	io_error(const std::string & reason, const std::string & filename) 
		: _reason(reason), _filename(filename) {}
	virtual ~io_error() throw() {}
	void filename(const std::string & n) throw()
		{ _filename = n; }
	const std::string & filename() const throw()
		{ return _filename; }
	const char * what() const throw() 
		{ _what = /*std::string("img::io_error ") + */_reason + ": " + _filename;
		  return _what.c_str(); }
protected:
	std::string _reason;
	std::string _filename;
private:
	mutable std::string _what;
};

class bad : public io_error {
public:
	enum reason_type {
		palette,		///< palette and 24/32 bit images
		palette_index	///< index out of range
	};
private:
	reason_type		_rsn;
	int				_value;
public:
	bad(reason_type r, int v = 0)
		: io_error("corrupt image file format", std::string()), _rsn(r), _value(v) { }
	bad(reason_type r, const std::string & filename, int v = 0)
		: io_error("corrupt image file format", filename), _rsn(r), _value(v) { }
	reason_type reason() const throw()
		{ return _rsn; }
	int value() const throw()
		{ return _value; }
};

class unsupported : public io_error {
public:
	enum reason_type {
		file_type,
		compression,
		bit_count,
		interleaving,
		width,
		height,
		no_palette		///< absence of palette and 8 bit images
	};
private:
	reason_type		_rsn;
	int				_value;
public:
	unsupported(reason_type r, int v = 0)
		: io_error("unsupported image file format mode", std::string()), _rsn(r), _value(v) { }
	unsupported(reason_type r, const std::string & filename, int v = 0)
		: io_error("unsupported image file format mode", filename), _rsn(r), _value(v) { }
	reason_type reason() const throw()
		{ return _rsn; }
	int value() const throw()
		{ return _value; }
};

} //namespace img

#endif //_io_error_h_
